'use strict'

function AxiosInstanceUtils (config) {
  this._init(config)
}

AxiosInstanceUtils.create = function (config) {
  return new AxiosInstanceUtils(config)
}

AxiosInstanceUtils.HEADER_KEY_CONTENT_TYPE = 'Content-Type'

AxiosInstanceUtils.CONTENT_TYPE_APPLICATION_JSON = 'application/json'

AxiosInstanceUtils.prototype = {
  $instance: null,
  REQUESTING: false,
  REQUESTING_MARK: {},
  DEFAULT_HEADERS: {},
  HEADERS: {},
  _init (config) {
    this.$instance = axios.create(config)
    this.setDefaultHeader(AxiosInstanceUtils.HEADER_KEY_CONTENT_TYPE, AxiosInstanceUtils.CONTENT_TYPE_APPLICATION_JSON)
    // 添加请求拦截器
    this.$instance.interceptors.request.use(config => {
      this.REQUESTING = true
      this.merge(this.DEFAULT_HEADERS, config.headers)
      this.merge(this.HEADERS, config.headers)
      this.clearHeader()
      return config
    }, function (error) {
      this.REQUESTING = false
      return Promise.reject(error)
    })
    // 添加响应拦截器
    this.$instance.interceptors.response.use(response => {
      this.REQUESTING = false
      return response.data
    }, error => {
      this.REQUESTING = false
      if (error.response) {
        if (error.response.data && error.response.data.message) {
        } else {
        }
      } else if (error.request) {
      } else {
      }
      return Promise.reject(error)
    })
  },
  get (url, params, success, error, flag) {
    if (!this.checkRequesting(flag)) {
      return false
    }
    this.$instance.get(url, {
      params: params
    })
      .then(responseData => {
        this.REQUESTING_MARK[flag] = false
        if (success) {
          success(responseData)
        }
      })
      .catch(err => {
        this.REQUESTING_MARK[flag] = false
        if (error) {
          error(err)
        }
      })
  },
  post (url, params, data, success, error, flag) {
    if (!this.checkRequesting(flag)) {
      return false
    }
    this.$instance.post(url, JSON.stringify(data), {
      params: params
    })
      .then(responseData => {
        this.REQUESTING_MARK[flag] = false
        if (success) {
          success(responseData)
        }
      })
      .catch(err => {
        this.REQUESTING_MARK[flag] = false
        if (error) {
          error(err)
        }
      })
  },
  delete (url, params, data, success, error, flag) {
    if (!this.checkRequesting(flag)) {
      return false
    }
    this.$instance.delete(url, {
      params: params,
      data: data
    })
      .then(responseData => {
        this.REQUESTING_MARK[flag] = false
        if (success) {
          success(responseData)
        }
      })
      .catch(err => {
        this.REQUESTING_MARK[flag] = false
        if (error) {
          error(err)
        }
      })
  },
  put (url, params, data, success, error, flag) {
    if (!this.checkRequesting(flag)) {
      return false
    }
    this.$instance.put(url, JSON.stringify(data), {
      params: params
    })
      .then(responseData => {
        this.REQUESTING_MARK[flag] = false
        if (success) {
          success(responseData)
        }
      })
      .catch(err => {
        this.REQUESTING_MARK[flag] = false
        if (error) {
          error(err)
        }
      })
  },
  checkRequesting (flag) {
    if (flag === true && this.REQUESTING) {
      Message({
        message: '请等待上一个请求完成',
        type: 'warning',
        showClose: true
      })
      return false
    } else if (flag && flag !== false && flag !== true && this.REQUESTING_MARK[flag]) {
      Message({
        message: '请勿重复请求',
        type: 'warning',
        showClose: true
      })
      return false
    } else if (flag && flag !== false && flag !== true) {
      this.REQUESTING_MARK[flag] = true
    }
    return true
  },
  setDefaultHeader (key, value) {
    this.DEFAULT_HEADERS[key] = value
  },
  clearDefaultHeader () {
    this.DEFAULT_HEADERS = {}
  },
  addHeader (key, value) {
    this.HEADERS[key] = value
  },
  clearHeader () {
    this.HEADERS = {}
  },
  merge (target, source) {
    for (let key in target) {
      source[key] = target[key]
    }
    return source
  }
}
